import {Injectable} from '@angular/core';

import {isBoolean, isNull} from '../helpers/object';

import {ScriptLoader} from './scriptLoader';

import {TMapInstance, MapOptions} from './../types/Map';
import { Overlay } from './../types/Overlay';
import { Control } from './../types/Control';

@Injectable()
export class MapService {
  private _map: Promise<TMapInstance>;

  private _mapResolver: (value: TMapInstance) => void;

  constructor(private _loader: ScriptLoader) {
    this._map = new Promise<TMapInstance>((resolve: () => void) => {
      this._mapResolver = resolve;
    });
  }

  public createMap(el: HTMLElement, opt?: MapOptions): Promise <TMapInstance> {

    return new Promise(resolve => {
        /**/
      this._loader
        .load(() => {
            // http://t0.tianditu.cn/img_w/wmts
            // http://t0.tianditu.cn/cva_c/wmts
            /*
            const imageURL = 'http://t0.tianditu.cn/img_w/wmts?' +
            'SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&LAYER=img&STYLE=default&TILEMATRIXSET=w&FORMAT=tiles' +
            '&TILEMATRIX={z}&TILEROW={y}&TILECOL={x}';
            */


            const imageURL = 'http://t3.tianditu.com/DataServer?T=img_w&x={x}&y={y}&l={z}';

        const lay = new window.T.TileLayer(imageURL, {minZoom: 1, maxZoom: 16});
/*
        const imageURL1 = 'http://t3.tianditu.com/DataServer?T=cva_w&x={x}&y={y}&l={z}';

        const lay1 = new window.T.TileLayer(imageURL1, {minZoom: 1, maxZoom: 16});
*/
        const imageURL2 = 'http://t4.tianditu.com/DataServer?T=cia_w&x={x}&y={y}&l={z}';

        const lay2 = new window.T.TileLayer(imageURL2, {minZoom: 1, maxZoom: 16});

        const config = {layers: [ lay, lay2 ]};

          const map = new window.T.Map(el, config/**/);
          console.log(opt);
          const lng = opt === undefined ? 116.40769 : opt.center.lng;
          const lat = opt === undefined ? 39.89945 : opt.center.lat;
          const zoom = opt === undefined ? 9 : opt.zoom;
          map.centerAndZoom(new window.T.LngLat(lng, lat), zoom);

          /*
          control = new T.Control.Zoom();
          //添加缩放平移控件
          map.addControl(control);
          */

          /*
          const control = new window.T.Control.Zoom();
          map.addControl(control);
          */

          this._mapResolver(map);

          resolve(map);
      });

    });
  }


  public addOverlay(
    cb: (map: TMapInstance) => Overlay
  ): Promise<{ map: TMapInstance; overlay: Overlay }> {
    return this._map.then((map: TMapInstance) => {
      const overlay = cb(map);
      map.addOverLay(overlay);
      return { map, overlay };
    });
  }

  public removeOverlay(overlay: Overlay): Promise<void> {
    return this._map.then((map: TMapInstance) => {
      map.removeOverLay(overlay);
    });
  }

  public addControl(control: Control): Promise<void> {
    return this._map.then((map: TMapInstance) => {
        map.addControl(control);
      });
  }

  public removeControl(control: Control): Promise<void> {
    return this._map.then((map: TMapInstance) => {
      map.removeControl(control);
    });
  }


}
